package cn.hp.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ShowUsernameController {
	
	//页面获取用户名
	@RequestMapping("/showUsername")
	public void showUsername(HttpServletRequest request){
		//获取session
		HttpSession session = request.getSession();
		//过去session中所有的name 
		Enumeration attributeNames = session.getAttributeNames();
		//Enumeration翻译为列举   通过这个接口中的方法可以枚举(一次获得一个)对象集合中的元素
		//遍历枚举
		while(attributeNames.hasMoreElements()){
			System.out.println(attributeNames.nextElement());
		}
		//SPRING_SECURITY_CONTEXT
		//SecurityContext  是安全框架的上下文对象
		SecurityContext securityContext = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		//获取认证信息
		Authentication authentication = securityContext.getAuthentication();
		//getPrincipal主要信息:用户信息
		User user=(User) authentication.getPrincipal();
		//获取用户名
		String username=user.getUsername();
		System.out.println("username");
		
		
		
		//获得安全框架的上下文对象方式二
		SecurityContext context = SecurityContextHolder.getContext();
		
	}
}
